package com.paradogs.framework.core.annoations.rpc;

import com.paradogs.framework.core.annoations.base.BaseScanner;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.type.classreading.MetadataReader;

import java.io.IOException;

/**
 * @author: yumi
 * @date: 2023/8/13  23:24
 * @Description: TODO
 */
public class PRRPCInterfaceScanner extends BaseScanner {

    public PRRPCInterfaceScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }

    /**
     * 是否为可添加 bean，返回接口类型
     * 可以用 TypeFilter 加，但 TypeFilter 只能指定类，未知的动态类无法处理
     * @param metadataReader
     * @return
     * @throws IOException
     */
    @Override
    protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
        return metadataReader.getClassMetadata().isInterface()
                && metadataReader.getAnnotationMetadata().hasAnnotation(PRClient.class.getName());
    }

    /**
     * @param beanDefinition 一个 bean
     * @return
     */
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isInterface()
                && beanDefinition.getMetadata().hasAnnotation(PRClient.class.getName());
    }

    @Override
    public boolean isNeedRegister() {
        return true;
    }
}
